/**
 * Created by aifece on 15/1/19.
 */
(function(){
  $(function(){
    var hour = parseInt($('.js_orderTime').data('hour'));
    var min  = parseInt($('.js_orderTime').data('min'));

    var setTime = setInterval(function(){
      min = min-1;
      if (min < 0) {
        min = 59;
        hour = hour-1;
      }
      if (hour > 0) {
        $('.js_orderTime').html(hour+'小时'+min+'分钟后');
      } else {
        $('.js_orderTime').html(min+'分钟后');
      }
      if (hour == 0 && min == 0) {
        $('.js_orderTime').closest('.once').html('<em>订单被关闭</em>');
        $('.js_editPayType').remove();
        $('.js_modifyAddress').remove();
        clearInterval(setTime);
      }
    }, 60000);

    // 地址
    function AddressCurr(option)
    {
      AddressJs.call(this, option);
    }

    for (var i in AddressJs.prototype) {
      AddressCurr.prototype[i] = AddressJs.prototype[i]
    }

    AddressCurr.prototype.setForm = function(obj) {
      var type = obj.data('type');
      var id = obj.attr('data-address_id');

      if (type == 'child') {
        obj.closest('li').addClass('pen-curr').siblings('li').removeClass('pen-curr');
        $('.js_addressFormDiv').show();
      } else if (id == 'new') {
        $('.js_addressFormDiv').show();
        this.sltChange(obj);
      } else {
        $('.js_addressFormDiv').hide();
        this.sltChange(obj);
      }

      if (id == 'new') {
        this.showInfo(0, 'new');
      } else {
        this.showInfo(id, 'edit');
      }
    }

    AddressCurr.prototype.sltChange = function(obj)
    {
      obj.siblings('li').removeClass('pen-curr');
      obj.addClass('pen-curr');
    }

    // 提示信息
    AddressCurr.prototype.inputMsg =  function(obj, msg) {
      obj.parent().addClass('error');
      obj.closest('label').addClass('crr');
      obj.closest('label').append('<a href="javascript:void(0);"><em><img src="'+siteUrl+'/images/flase-icon.png"/></em>'+msg+'</a>');
    }

    AddressCurr.prototype.deleteMsg = function(obj){
      obj.parent().removeClass('error');
      obj.closest('label').removeClass('crr');
      obj.closest('label').find('a').remove();
    }

    AddressCurr.prototype.setInfo = function(type, id, info)
    {
      if (!id) {
        return;
      }
      var _this = this;
      var orderId = $(_this.option.formDiv).attr('data-order_id');

      if (type == 'new') {
        $(_this.option.listDiv).find('li').removeClass('pen-curr');
        $(_this.option.listDiv).prepend('<li data-address_id="'+id+'" class="js_addressEdit pen-curr"><em class="ico"></em>' + info + '<span data-type="child" data-address_id="'+id+'">编辑</span></li>');
      } else {
        $(_this.option.listDiv).find('li').each(function() {
          if ($(this).attr('data-address_id') == id) {
            $(this).html('<em class="ico"></em>' + info+'<span data-type="child" data-address_id="'+id+'">编辑</span>');
            _this.sltChange($(this));
          }
        });
      }
      $('.js_addressInfo').attr('data-address_id', id);
      getExpress();

      $('.js_payTypeInfo').addClass('hidden');
      $('.js_payTypeSlt').parent().removeClass('hidden');
    }

    new AddressCurr();

    $('.js_ADDIsDefault').on('click', function(){
      var isDef = $(this).hasClass('skin-check');
      if (isDef) {
        $(this).removeClass('skin-check');
      } else {
        $(this).addClass('skin-check');
      }
    });

    $('.js_editPayType').on('click', function(){
      $('.js_payTypeInfo').addClass('hidden');
      $('.js_payTypeSlt').parent().removeClass('hidden');
    });

    $('.js_payTypeSlt li').on('click', function(){
      var payType = $(this).attr('data-pay_type');
      var title   = $(this).attr('data-title');
      $(this).siblings().removeClass('pay-curr');
      $(this).addClass('pay-curr');

      $('.js_savePayType').attr('data-pay_type', payType);
      $('.js_savePayType').attr('data-title', title);
    });

    $('.js_expressSlt li').on('click', function(){
      var expressId = $(this).attr('data-express_id');
      var expressTitle = $(this).attr('data-express_title');
      if ($(this).hasClass('disabled')) {
        return false;
      }
      $(this).addClass('pay-curr').siblings().removeClass('pay-curr');

      $('.js_savePayType').attr('data-express_id', expressId);
      $('.js_savePayType').attr('data-express_title', expressTitle);
    });

    /**
     * 保存支付方式
     */
    $('.js_savePayType').on('click', function(){
      var payType   = $(this).attr('data-pay_type');
      var title     = $(this).attr('data-title');
      var orderId   = $(this).attr('data-order_id');
      var url       = siteUrl+'/order/edit_order_info';
      var addressId = $('.js_addressInfo').attr('data-address_id');
      var expressId = $('.js_savePayType').attr('data-express_id');
      
      /*
      var expressError = $('.js_savePayType').attr('data-express_error');
      var expressTitle = $('.js_savePayType').attr('data-express_title');

      if (expressError) {
        showMsg(expressError+',不能修改');
        return;
      }*/

      if (btnClick() === false) {
        return false;
      }
      $.post(url, { pay_type:payType, address_id:addressId , express_id:expressId, order_id:orderId }, function(json){
        if (json.info == 'ok') {
          $('.js_payTypeInfo').removeClass('hidden');
          $('.js_payTypeSlt').parent().addClass('hidden');

          $('.js_payTypeInfo').find('.pay-type').html(title+'支付');
          $('.js_subOrder').attr('data-href', json.url);
          $('.js_expressTitle').html(expressTitle);

          $('.js_orderExpressPrice').html(json.express_price+'元');
          $('.js_orderTotalPrice').html(json.order_price);
        } else {
          showMsg(json.info, 'error');
        }
        btnClick('reset');
      }, 'json');
    });

    /**
     * 取消订单
     */
    $('.js_cancelOrder').on('click', function(){
      var orderId = $(this).data('order_id');
      $('.js_cancelOrderConfirm').show().prev().show();
      $('.js_cancelOrderConfirm').data('order_id', orderId);
    });

    $('.js_cancelBtn').on('click', function(){
      $('.js_cancelOrderConfirm').hide().prev().hide();
    });

    $('.js_submitBtn').on('click', function(){
      var reason  = $('#js_cancelOrderReason').val();
      var orderId = $('.js_cancelOrderConfirm').data('order_id');
      var url     = siteUrl+'/order/cancel_order';
      
      if (reason == 0) {
        showMsg('请选择原因');
        return;
      }

      if (btnClick() === false) {
        return false;
      }
      $.post(url, { order_id:orderId, reason:reason }, function(json){
        if (json.info == 'ok') {
          location.href = location.href;
        } else {
          showMsg(json.info, 'error');
        }
        btnClick('reset');
      }, 'json');
    });

    getExpressInfo()
  });
  // 自动获取物流信息
  function getExpressInfo()
  {
    var orderId = $('.js_expressInfo').data('order_id');
    var url     = siteUrl+'/express/ajax_express_info';
    var date    = new Date();
    var isView  = '';
      if (date.getMonth() < 9) {
        isView  = '0';
      }
    var tmpHtml = '<div class="nota js_expressTmpInfo"><span>'+date.getFullYear()+'-'+isView+(date.getMonth()+1)+'-'+date.getDate()+'</span><span>'+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()+'</span><span>快递信息查询中...</span></div>';
    $('.js_expressInfo').after(tmpHtml);
    $.get(url, { order_id:orderId }, function(json){
      var infoA = $('.js_expressInfo').find('a');
      var html = infoA.html();
      if (infoA.length > 0) {
        infoA.remove();
        $('.js_expressInfo').append('<span>'+html+'</span>');
      }
      if (json.info == 'ok') {
        var newHtml = '';
        var data = json.json.data;
        var dataLenght = data.length;
        for (var i = 0; i < dataLenght; i++) {
          var time = data[i]['time'].split(' ');
          if (i+1 == dataLenght && infoA.length > 0) {
            newHtml = newHtml + '<div class="nota"><span>'+time[0]+'</span><span>'+time[1]+':00</span><a href="javascript:;">'+data[i]['context']+'</a></div>';
          } else {
            newHtml = newHtml + '<div class="nota"><span>'+time[0]+'</span><span>'+time[1]+':00</span><span>'+data[i]['context']+'</span></div>';
          }
        }
      } else {
        var newHtml = '<div class="nota"><span>'+date.getFullYear()+'-'+isView+(date.getMonth()+1)+'-'+date.getDate()+'</span><span>'+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()+'</span><a href="javascript:;">快递信息查询失败</a></div>';
      }
      $('.js_expressTmpInfo').remove();
      $('.js_expressInfo').after(newHtml);
    }, 'json');
  }

  // 自动获取快递
  function getExpress()
  {
    return false;
    var addressId     = $('.js_addressInfo').attr('data-address_id');
    var url           = siteUrl+'/express/express_price_for_order';
    var suerExpressId = $('.js_savePayType').attr('data-express_id');
    var orderId       = $('.js_addressForm').attr('data-order_id');

    $('.js_expressSlt').find('li').removeClass('pay-curr').addClass('disabled');
    $('.js_expressSlt').attr('data-is_suer', 0);

    $('.js_expressSlt li').each(function(){
      var expressId = $(this).attr('data-express_id');
      var that = $(this);
      var title = $(this).attr('data-express_title');
      $(this).find('span').html('运费计算中，请稍等');
      $.get(url, { express_id:expressId, address_id:addressId, order_id:orderId }, function(json){
        if (json.info == 'ok') {
          that.removeClass('disabled');
          that.find('span').html('运费：'+json.price+'元');
          var isSure = $('.js_expressSlt').attr('data-is_suer');
          if ((!that.siblings().hasClass('pay-curr') && isSure == 0) || expressId == suerExpressId) {
            $('.js_expressSlt').attr('data-has_error', 1)
            if (expressId == suerExpressId) { //如果相同 先固定
              $('.js_expressSlt').attr('data-is_suer', 1);
              $('.js_expressSlt').find('li').removeClass('pay-curr');
            }

            that.addClass('pay-curr');
            $('.js_savePayType').attr('data-express_id', expressId);
            $('.js_savePayType').attr('data-express_title', title);
            $('.js_savePayType').attr('data-express_error', '');

            $('.js_orderExpressPrice').html(json.price+'.00元');
            $('.js_orderTotalPrice').html(json.order_price);
          }
        } else {
          var hasError = $('.js_expressSlt').attr('data-has_error');
          if (hasError != 1) {
            $('.js_savePayType').attr('data-express_error', json.info);
          }
          that.addClass('disabled');
          that.find('span').html(json.info);
        }
      }, 'json');
    });
  }
})();